July 30, 2018

Introduction

Goal

The goal of the application is to predict a diamond price using a prediction linear model.

Also we will try to understand how different factors may influence the price of a diamond.

To address that we use the diamonds dataset containing the prices and other attributes of almost 54,000 diamonds.

To make interpretation easier we choose a random sample of maximum ten thousand entries from the original dataset.

Plotting the Data

We plot the diamond weight(carat) vs. price in dollars (price) along the x/y axes. We also plot the prediction model in red.

Code Sample

The following is a sample code from Shiny application 'server.R':

  # Calculate price prediction based on linear model and input carat
  diamond_pred <- reactive({
    diamond_model <- lm(price ~ carat, data = small_diamonds())
    predict(diamond_model, newdata = data.frame(
      carat = input$vcarat))
  })
  # Draw the plot Diamond price vs. carat
  output$diamondsPlot <- renderPlot({
    ggplot(small_diamonds(), aes(x=carat,y=price)) +
      geom_point(color='blue',fill='blue') +
      geom_smooth(method = "lm", color = "red", se = FALSE) +
      geom_point(aes(x=input$vcarat, y=diamond_pred()), 
                 color='red', fill='red', size = 4)
    
  })